# vPresent is an immersive presentation creation and display application.
# Copyright (C) 2007 by Infiscape Corporation
#
# This program is free software; you can redistribute it and/or modify it
# under the terms of the GNU General Public License as published by the Free
# Software Foundation; either version 2 of the License, or (at your option)
# any later version.
#
# This program is distributed in the hope that it will be useful, but WITHOUT
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
# FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
# more details.
#
# You should have received a copy of the GNU General Public License along
# with this program; if not, write to the Free Software Foundation, Inc.,
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.

from PyQt4 import QtCore, QtXml


def loadXMLDocument(fileName, openMode = QtCore.QIODevice.ReadWrite,
                    errMsgString = None):
   xml_file = QtCore.QFile(fileName)

   if not xml_file.open(openMode):
      msg = getFileErrorMessage(xml_file.error())

      if errMsgString is None:
         errMsgString = fileName

      raise Exception(msg % errMsgString)

   xml_doc = QtXml.QDomDocument()
   parsed, err_msg, err_line, err_col = xml_doc.setContent(xml_file, True)

   if not parsed:
      # TODO: Make this more specific
      raise Exception("%s at line %d, column %d" % (err_msg, err_line,
                                                    err_col))

   return xml_doc

def getFileErrorMessage(errorCode):
   msg = "An error occurred opening %s"

   if QtCore.QFile.NoError == errorCode:
      msg = "No error opening %s"
   elif QtCore.QFile.ReadError == errorCode:
      msg = "An error occured when reading from %s"
   elif QtCore.QFile.WriteError == errorCode:
      msg = "An error occured when writing to %s"
   elif QtCore.QFile.FatalError == errorCode:
      msg = "A fatal error occurred with %s"
   elif QtCore.QFile.ResourceError == errorCode:
      msg = "A resource error occurred with %s"
   elif QtCore.QFile.OpenError == errorCode:
      msg = "The file %s could not be opened"
   elif QtCore.QFile.AbortError == errorCode:
      msg = "The operation on %s was aborted"
   elif QtCore.QFile.TimeOutError == errorCode:
      msg = "The operation on %s timed out"
   elif QtCore.QFile.UnspecifiedError == errorCode:
      msg = "An unspecified error occurred when accessing %s"
   elif QtCore.QFile.RemoveError == errorCode:
      msg = "The file %s could not be removed"
   elif QtCore.QFile.RenameError == errorCode:
      msg = "The file %s could not be renamed"
   elif QtCore.QFile.PositionError == errorCode:
      msg = "The position in the file %s could not be changed"
   elif QtCore.QFile.ResizeError == errorCode:
      msg = "The file %s could not be resized"
   elif QtCore.QFile.PermissionError == errorCode:
      msg = "The file %s could not be accessed"
   elif QtCore.QFile.CopyError == errorCode:
      msg = "The file %s could not be copied"

   return msg
